//
//  ChangePsdCell.m
//  Hxcsfw
//
//  Created by qiysoft on 2017/11/14.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "ChangePsdCell.h"

@implementation ChangePsdCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.OKBtn.layer.masksToBounds = YES;
    self.OKBtn.layer.cornerRadius = 5;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)OKBtn:(id)sender {
    [self requestChangePassword];
}

//登录请求
- (void)requestChangePassword {

    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"mine",@"content":@{@"revert":@"chgpwd",@"username":[WShareUtil sharedData].dicSession[@"nsrsbh"],@"password":self.TF1.text,@"newpwd":self.TF2.text,@"confirmpwd":self.TF3.text}};

    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};


    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];

        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if ([successStr boolValue]) {
            //            if (self.delegate && [self.delegate respondsToSelector:@selector(btnOfVerifyLoginClicked)]) {
            //                [ self.delegate btnOfVerifyLoginClicked];
            //            }
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil];
//            [WShareUtil sharedData].dicSession = returnDic;
            
            if([returnDic[@"resultCode"] isEqualToString:@"SUCCEED"]){
                
                UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:returnDic[@"errorInfo"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [av show];
                
            }
            
        } else {
            
            if([returnDic[@"resultCode"] isEqualToString:@"PARAMS_ERROR"]){
                
                UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:returnDic[@"errorInfo"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [av show];
                
            }else if ([returnDic[@"resultCode"] isEqualToString:@"UNLIKE_PASSWORD"]){
                
                UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:returnDic[@"errorInfo"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [av show];
            }else if ([returnDic[@"resultCode"] isEqualToString:@"NO_PARAMS"]){
                
                UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请完善修改信息!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [av show];
            }
        }
        
    } failure:^(NSError *error) {
        
        [SVProgressHUD showImage:nil status:@"修改失败"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
        [SVProgressHUD dismissWithDelay:1.];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        
    } showProgress:YES];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        [self endEditing:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeSuccessOK" object:nil];//修改成功pop的通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LogOut" object:nil];//修改成功退出登录的通知
    }
}

@end
